package com.sugar.subscribe.course.module.business.controller.admin.coachplan;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.coachplan.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachplan.CoachPlanDO;
import com.sugar.subscribe.course.module.business.service.coachplan.CoachPlanService;

@Tag(name = "管理后台 - 教练课程方案")
@RestController
@RequestMapping("/business/coach-plan")
@Validated
public class CoachPlanController {

    @Resource
    private CoachPlanService coachPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建教练课程方案")
    //@PreAuthorize("@ss.hasPermission('business:coach-plan:create')")
    public CommonResult<Long> createCoachPlan(@Valid @RequestBody CoachPlanSaveReqVO createReqVO) {
        return success(coachPlanService.createCoachPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教练课程方案")
    //@PreAuthorize("@ss.hasPermission('business:coach-plan:update')")
    public CommonResult<Boolean> updateCoachPlan(@Valid @RequestBody CoachPlanSaveReqVO updateReqVO) {
        coachPlanService.updateCoachPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教练课程方案")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('business:coach-plan:delete')")
    public CommonResult<Boolean> deleteCoachPlan(@RequestParam("id") Long id) {
        coachPlanService.deleteCoachPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除教练课程方案")
                //@PreAuthorize("@ss.hasPermission('business:coach-plan:delete')")
    public CommonResult<Boolean> deleteCoachPlanList(@RequestParam("ids") List<Long> ids) {
        coachPlanService.deleteCoachPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教练课程方案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('business:coach-plan:query')")
    public CommonResult<CoachPlanRespVO> getCoachPlan(@RequestParam("id") Long id) {
        CoachPlanDO coachPlan = coachPlanService.getCoachPlan(id);
        CoachPlanRespVO respVO = BeanUtils.toBean(coachPlan, CoachPlanRespVO.class);
        return success(respVO.setServiceIds(new Long[]{coachPlan.getServicePId(), coachPlan.getServiceId()}));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教练课程方案分页")
    //@PreAuthorize("@ss.hasPermission('business:coach-plan:query')")
    public CommonResult<PageResult<CoachPlanRespVO>> getCoachPlanPage(@Valid CoachPlanPageReqVO pageReqVO) {
        return success(coachPlanService.getCoachPlanPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教练课程方案 Excel")
    //@PreAuthorize("@ss.hasPermission('business:coach-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoachPlanExcel(@Valid CoachPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "教练课程方案.xls", "数据", CoachPlanRespVO.class,
                coachPlanService.getCoachPlanPage(pageReqVO).getList());
    }

}